
#include "log.h"
#include "socket.h"
#include "iomanager.h"
#include <string>

static johnsonli::Logger::ptr g_logger = LOG_ROOT();

void test()
{
    //创建地址
    johnsonli::IPAddress::ptr addr = johnsonli::Address::LookupAnyIPAddress("www.baidu.com:80");
    if(addr)
    {
        LOG_INFO(g_logger) << "addr=" << addr->toString();
    }
    else
    {
        LOG_INFO(g_logger) << "LookupAnyIPAddress fail";
    }
    //addr->setPort(80);

    //创建socket
    johnsonli::Socket::ptr socket = johnsonli::Socket::CreateTCP(addr);
    //连接
    socket->connect(addr);

    //发送数据
    const char buf[] = "GET / HTTP/1.1\r\n\r\n";
    int rt = socket->send(buf, sizeof(buf));
    if(rt <= 0)
    {
        LOG_INFO(g_logger) << "send fail";
        return;
    }

    //接收数据
    std::string buffers;
    buffers.resize(4096);
    rt = socket->recv(&buffers[0], 4096);
    if(rt <= 0)
    {
        LOG_INFO(g_logger) << "recv fail";
        return;
    }

    LOG_INFO(g_logger) << buffers;


}

int main()
{
    johnsonli::IOManager iom;
    iom.schedule(test);
    return 0;
}